/**
 * Created by liubin on 2015/09/30.
 */

import { Map, List, fromJS } from 'immutable';
import { SELECT_COMBO, SELECT_ROLE_LIST_PAGING, ADD_ROLE, REMOVE_ROLE, GET_ROLE, MODIFY_ROLE } from 'actions/roleAction';
import { SUCCESS } from 'actions/baseAction';

const initialState = Map({
    roleList: List(),
    comboList: List()
});

/**
 * 权限得Reducer
 */
export function roleStore(state = 'DEFAULT', action = {type: 'DEFAULT'}) {
    switch (action.type) {
        case SELECT_COMBO:
            {
                if (action.status === SUCCESS) {
                    state = state.set('comboList', fromJS(action.data));
                    // 延时回调
                    const { callback } = action;
                    if (callback) {
                        setTimeout(() => {
                            callback();
                        }, 500);
                    }
                }
                return state;
            }
        case SELECT_ROLE_LIST_PAGING:
            {
                const data = action.data.map(value => {
                    value.op = '';
                    return value;
                });
                state = state.set('roleList', fromJS(action.data));
                if (action.callback) {
                    setTimeout(() => {
                        action.callback({
                            draw: action.draw,
                            recordsTotal: action.total,
                            recordsFiltered: action.total,
                            data
                        });
                    }, 0);
                }
                return state;
            }
        case ADD_ROLE:
            {
                const { status, callback } = action;
                if (status === SUCCESS) {
                    if (callback) {
                        setTimeout(() => {
                            callback();
                        }, 500);
                    }
                }
                return state;
            }
        case REMOVE_ROLE:
            {
                const { status, callback } = action;
                if (status === SUCCESS) {
                    if (callback) {
                        setTimeout(() => {
                            callback();
                        }, 500);
                    }
                }
                return state;
            }
        case GET_ROLE:
            {
                const { status, callback, data } = action;
                if (status === SUCCESS) {
                    if (data) {
                        state = state.set('role', fromJS(data));
                        if (callback) {
                            setTimeout(() => {
                                callback();
                            }, 500);
                        }
                    }
                }
                return state;
            }
        case MODIFY_ROLE:
            {
                const { status, data, message, d } = action;
                if (status === SUCCESS) {
                    state = state.set('role', data);
                    if (d) {
                        setTimeout(() => {
                            d.resolve();
                        }, 500);
                    }
                } else {
                    if (d) {
                        setTimeout(() => {
                            d.reject(message);
                        }, 500);
                    }
                }
                return state;
            }
        default:
            return state === 'DEFAULT' ? initialState : state;
    }
}
